﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDom;
using System.Web.Mvc.Html;
using Bzure.Apps.Security.ViewModels;

namespace Bzure.Apps.Security.Views
{
    public class Register : PageView<RegisterViewModel>
    {
        public virtual ContainerTag TitleContent(ContainerTag container)
        {
            return container[Model.Title];
        }
        string Script1()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" $().ready(function () {");
            sb.Append(" $(\"#Register_Form\").validate({");
            sb.Append("rules: {UserName: {required: true, minlength: 2},");
            sb.Append("Password: {required: true, minlength: 5},");
            sb.Append(" ConfirmPassword: {required: true,minlength: 5, equalTo: \"#Password\"  },");
            sb.Append(" EMail: {required: true,email: true}");
            sb.Append("},");
            sb.Append("messages: {UserName: {required: \"请输入用户名\", minlength: \"用户名至少需要两个字符\"},");
            sb.Append("ConfirmPassword: { required: \"请确认密码\", minlength: \"密码至少需要5个字符\", equalTo: \"两次输入密码不一致\"},");
            sb.Append("Password: {required: \"请输入密码\", minlength: \"密码至少需要5个字符\"},EMail: \"请输入有效的邮件地址\"}");
            sb.Append("  });});");
            return sb.ToString();
        }
        public virtual ContainerTag MainContent(ContainerTag container)
        {
            return container
            [
                      script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery.validate.min.js" })),
                    script.attr(type: MimeType.text.javascript)
                    [
                         Script1()
                    ],
                    form.attr(id: "Register_Form", method: Method.post)
                    [
                     h1[Model.Title],
                    p.attr(id: "Register_UserName")
                             [
                               label.attr(@class: "labelTitle", @for: "UserName")["用户名"],
                               input.attr(type: InputType.text, id: "UserName", name: "UserName").ext("autocomplete", "off")
                             ],
                     p.attr(id: "Register_password")
                             [
                               label.attr(@class: "labelTitle", @for: "password")["密码"],
                               input.attr(type: InputType.password, id: "Password", name: "Password").ext("autocomplete", "off")
                             ],
                      p.attr(id: "Register_ConfirmPassword")
                             [
                               label.attr(@class: "labelTitle", @for: "ConfirmPassword")["确认密码"],
                               input.attr(type: InputType.password, id: "ConfirmPassword", name: "ConfirmPassword").ext("autocomplete", "off")
                             ],
                     p.attr(id: "Register_Email")
                             [
                               label.attr(@class: "labelTitle", @for: "EMail")["邮箱"],
                               input.attr(id: "EMail", name: "EMail").ext("type", "EMail").ext("autocomplete", "off")
                             ],
                      p.attr(id: "Register_Submit")
                             [
                             input.attr(type: InputType.submit, value: "注册", @class: "submit")
                             ],
                             If(!string.IsNullOrEmpty(this.Model.Message), Then: c => c
                                    [
                                      p.attr(id: "Register_Message", @class: "MessageBox")[this.Model.Message]
                                    ]
                               )
                    ]
             ];
        }

        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return ReusableCreateTagTree(TitleContent, MainContent, Model.MasterViewModel);
        }
    }
}
